package com.yylending.las.main;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yylending.las.Global;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by ying_he on 2017/9/5.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class InviteCodeVerifyModel extends Model {
	private static final String TAG = InviteCodeVerifyModel.class.getSimpleName();
	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		switch (functionalCode){
			case InviteCodeVerifyPresenter.FUNCTION_GET_INVITE_CODE_STATUS:
				return service.getInviteCode(Global.LOGIN_TOKEN, new EmptyEntity());
			case InviteCodeVerifyPresenter.FUNCTION_SUBMIT_INVITE_CODE_STATUS:
				return service.submitInviteCode(Global.LOGIN_TOKEN, (SubmitInviteCodeParams) params);
			default:
				return null;
		}
	}

	@Override
	public void onNext(Object entity) {
		BaseResultEntity result = cast(entity);
		switch (result.requestCode) {
			case InviteCodeVerifyPresenter.FUNCTION_GET_INVITE_CODE_STATUS:
				GetInviteCodeStatusResult getInviteCodeStatusResult = cast(entity);
				EventAgent.post(getInviteCodeStatusResult);
				break;
			case InviteCodeVerifyPresenter.FUNCTION_SUBMIT_INVITE_CODE_STATUS:
				SubmitInviteCodeResult submitInviteCodeResult = cast(entity);
				EventAgent.post(submitInviteCodeResult);
				break;
			default:
				break;
		}
	}

	private interface Service {
		@POST("server/customer/getInvitationStatus")
		Observable<GetInviteCodeStatusResult> getInviteCode(@Header("token") String token, @Body EmptyEntity params);

		@POST("server/customer/inviteCodeVerify")
		Observable<SubmitInviteCodeResult> submitInviteCode(@Header("token") String token, @Body SubmitInviteCodeParams params);
	}

	static class GetInviteCodeStatusResult extends BaseResultEntity<EmptyEntity>{

	}

	static class SubmitInviteCodeResult extends BaseResultEntity<EmptyEntity>{

	}

	static class SubmitInviteCodeParams{
		String inviteCode = "";
	}
}
